import Application from 'koa';
import { ResponseState, SendAction } from '../types/send';

export enum CodeState {
    SUCCESS = 0,
    FAILED = 1,
    BADREQUEST = 2,
    AuthenticationFAILED = 3,
}

/**
 * 注入简单的返回函数
 *
 * @param app Application
 */
export default function send(app: Application) {
    app.use(async (ctx, next) => {
        const sendAction: SendAction = {
            send(data) {
                ctx.body = data;
            },
            sendF() {
                ctx.body = {
                    status: CodeState.FAILED,
                } as ResponseState;
            },
            sendFM(msg) {
                ctx.body = {
                    status: CodeState.FAILED,
                    msg,
                } as ResponseState;
            },

            sendS() {
                ctx.body = {
                    status: CodeState.SUCCESS,
                };
            },

            sendSD(data) {
                ctx.body = {
                    data,
                    status: CodeState.SUCCESS,
                } as ResponseState;
            },

            sendSDM(data, msg) {
                ctx.body = {
                    data,
                    msg,
                    status: CodeState.SUCCESS,
                };
            },
        };
        Object.assign(ctx, sendAction);
        await next();
    });
}
